package com.lee.common.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;


import java.util.Objects;


/**
 * @Description: SharedPreferances工具类
 */
public class SharedPreUtils {

	/**
	 * 全局shared preference的名称
	 */

	public static void setInteger(String key, int value) {
		SharedPreferences sp = Objects.requireNonNull(InitUtils.Companion.getContext()).getSharedPreferences(
				ConstantsKt.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putInt(key, value);
		editor.apply();
	}

	public static int getInteger(String key, int defaultValue) {
		SharedPreferences sp = Objects.requireNonNull(InitUtils.Companion.getContext()).getSharedPreferences(
				ConstantsKt.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
		return sp.getInt(key, defaultValue);
	}

	public static void setString(String key, String value) {
		SharedPreferences sp = Objects.requireNonNull(InitUtils.Companion.getContext()).getSharedPreferences(
				ConstantsKt.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putString(key, value);
		editor.apply();
	}

	public static String getString(String key) {
		SharedPreferences sp = Objects.requireNonNull(InitUtils.Companion.getContext()).getSharedPreferences(
				ConstantsKt.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
		return sp.getString(key, "");
	}

	public static void setBoolean(String key, boolean value) {
		SharedPreferences sp = Objects.requireNonNull(InitUtils.Companion.getContext()).getSharedPreferences(
				ConstantsKt.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
		Editor editor = sp.edit();
		editor.putBoolean(key, value);
		editor.apply();
	}

	public static boolean getBoolean(String key,
                                     boolean defaultValue) {
		SharedPreferences sp = Objects.requireNonNull(InitUtils.Companion.getContext()).getSharedPreferences(
				ConstantsKt.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
		return sp.getBoolean(key, defaultValue);
	}

}
